package cn.chrisx.drad.admob;

import android.content.Context;

import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;

/**
 * @author Jzfox <jz_boy@163.com>
 * @version 1.0
 * @date 2018/9/26 12:00
 * @description
 * @copyright ©2018 www.jzfox.net All rights reserved.
 */
public class RewardedVideoImpl extends BaseAd implements RewardedVideoAdListener {

    private RewardedVideoAd rewardedVideoAd;

    @Override
    public boolean loadAd(Context context) {
        super.loadAd(context);

        if (rewardedVideoAd == null) {
            rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(context);
            rewardedVideoAd.setRewardedVideoAdListener(this);
        }

        rewardedVideoAd.loadAd(drAdConfig.getAdUnitId(), adRequest);

        return true;

    }

    @Override
    public boolean show() {

        if (isValid()) {
            rewardedVideoAd.show();
            return true;
        }

        return false;
    }


    @Override
    public boolean isValid() {
        return rewardedVideoAd != null && rewardedVideoAd.isLoaded();
    }

    @Override
    public void onRewardedVideoAdLoaded() {
        googleAdListener.onAdLoaded();
    }

    @Override
    public void onRewardedVideoAdOpened() {
        googleAdListener.onAdOpened();

    }

    @Override
    public void onRewardedVideoStarted() {

    }

    @Override
    public void onRewardedVideoAdClosed() {
        googleAdListener.onAdClosed();
    }

    @Override
    public void onRewarded(RewardItem rewardItem) {
        onReward(true, rewardItem);
    }

    @Override
    public void onRewardedVideoAdLeftApplication() {
        googleAdListener.onAdLeftApplication();
    }

    @Override
    public void onRewardedVideoAdFailedToLoad(int i) {
        googleAdListener.onAdFailedToLoad(i);
    }

    @Override
    public void onRewardedVideoCompleted() {

    }
}
